﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
namespace Reply
{
    public partial class Form1 : Form
    {
        byte DataSended = 0;
        byte[] DataToSend = new byte[] { 0x01, 0x02, 0x03 };                                               //数据发送
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived);       //添加串口中断事件
        }

        private void SetOvlShape(int which)                                                               //填充颜色   
        {
            switch(which)
            {
                case 0x01:
                    ovalShape1.FillColor = Color.Green;
                    break;
                case 0x11:
                    ovalShape1.FillColor = Color.Red;
                    break;
                case 0x02:
                    ovalShape2.FillColor = Color.Green;
                    break;
                case 0x12:
                    ovalShape2.FillColor = Color.Red;
                    break;
                case 0x03:
                    ovalShape3.FillColor = Color.Green;
                    break;
                case 0x13:
                    ovalShape3.FillColor = Color.Red;
                    break;
                case 0x04:
                    ovalShape1.FillColor = Color.Green;
                    ovalShape2.FillColor = Color.Green;
                    ovalShape3.FillColor = Color.Green;
                    break;
                case 0x14:
                    ovalShape1.FillColor = Color.Red;
                    ovalShape2.FillColor = Color.Red;
                    ovalShape3.FillColor = Color.Red;
                    break;
                default:
                    break;
            }
        }

        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte DataReceived = (byte)(serialPort1.ReadByte());                                           //单字节读取
            SetOvlShape(DataReceived);
        }

        private void button1_Click(object sender, EventArgs e)                                          //打开/关闭串口
        {
            if (serialPort1.IsOpen)                                                                     //一堆处理……
            {
                try
                {
                    serialPort1.Close();
                }
                catch
                {

                }
                button1.Text = "打开串口";
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;                                              //串口号    
                    serialPort1.Open();                                                                 //打开
                }
                catch
                {
                    MessageBox.Show("串口打开错误，请检查", "串口");
                }
                button1.Text = "关闭串口";
            }
        }

        private void SendDataToSerialPort(SerialPort MyPort, byte DataToSend)                            //单字节发送数据    
        {
            byte[] DatasToWrite = new byte[] { DataToSend };                                               //数据包
            if (serialPort1.IsOpen)
            {
                try
                {
                    MyPort.Write(DatasToWrite, 0, 1);                                                  //发数据
                }
                catch
                {
                    MessageBox.Show("串口数据写入错误", "错误");
                }
            }
        }

        private void Button_Click(object sender, EventArgs e)                                         //三个按键共用一个处理函数
        {
            Button MyButton = (Button)sender;                                                         //通过tag属性来区分
            DataSended = Convert.ToByte(MyButton.Tag);
            SendDataToSerialPort(serialPort1, DataToSend[DataSended - 1]);                             //发数据
        }
    }
}
